using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Xml;
using System.IO;

namespace Destra.Tools.DACGenerator
{
	[RunInstaller(true)]
	public partial class AddInInstaller : Installer
	{
		public AddInInstaller()
		{
			InitializeComponent();

			this.AfterInstall += new InstallEventHandler(AddInInstaller_AfterInstall);
		}

		void AddInInstaller_AfterInstall(object sender, InstallEventArgs e)
		{
			string path = Path.Combine(
				Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
				@"Visual Studio 2005\Addins\DACGenerator.AddIn");

			if (File.Exists(path))
			{
				XmlDocument addinDocument = new XmlDocument();
				addinDocument.Load(path);

				XmlNamespaceManager namespaceManager = 
					new XmlNamespaceManager(addinDocument.NameTable);

				namespaceManager.AddNamespace("ae",
					"http://schemas.microsoft.com/AutomationExtensibility");

				XmlNode node = addinDocument.SelectSingleNode(
					"ae:Extensibility/ae:Addin/ae:Assembly",
					namespaceManager);

				if (node != null)
				{
					node.InnerText = System.Reflection.Assembly.GetExecutingAssembly().Location;

					addinDocument.Save(path);
				}
			}
		}
	}
}